package com.util;

import android.os.Handler;
import android.util.Log;

import com.http.RegTask;
import com.http.RegTask.CallBack;
import com.http.UserInfoController;
import com.http.UserInfoTask;
import com.modle.User;

public class UserUtil {
	public static User user = null;

	/**
	 * 
	 * @Description:TODO读取登录用户信息,先判断内存里是否有，如果有直接返回，否则从文件中读取
	 * @return
	 * @return User
	 * @author zhuw
	 * @date 2013-4-25 下午4:59:21
	 */
	public static User readUser() {
		User rUser;
		if (user != null) {
			// 先读取内存
			rUser = user;
		} else {
			// 读取文件，不为空则先更新内存再返回
			rUser = SpUserUtil.readUser();
			if (rUser != null) {
				user = rUser;
			}
		}
		return rUser;

	}

	/**
	 * 
	 * @Description:TODO保存用户信息到内存及文件
	 * @param u
	 * @return void
	 * @author zhuw
	 * @date 2013-4-25 下午5:04:09
	 */
	public static void saveUser(final User u) {
			myUser = u;
		new UserInfoTask(new UserInfoTask.CallBack() {
			
		
			public void procError(String msg) {
				Log.i("test", "登录错误");
				reg(u);
			}
			
			@Override
			public void procUserInfoError(String msg) {
				Log.i("test", "登录错误");
				reg(u);
			}
		
			public void procUserInfoData(User data) {
				user = data;
				Log.i("test", "登录并保存成功");
				SpUserUtil.saveUser(data);
				
			}
		}).execute(u.id);
	
	}
	private static User myUser;
	private static Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			
			if (msg.what == 999) {
				switch (msg.arg1) {
				case 1:
					String resut =  (String) msg.obj;
					if ("sucess".endsWith(resut)) {
						if (myUser != null) {
							saveUser(myUser);
						}
					}else if ("fail".endsWith(resut)) {//失败
						
					}
					break;

				default:
					break;
				}
				saveUser(myUser);
				
			}
		};
	};
	private static int count = 0;
	private static void reg(final User u){
		if (u == null) {
			return;
		}
		String phone = u.getMobile();
		String password = phone.substring(7,11);
		Log.i("test", "注册自己的服务器="+phone+"密码="+password);
		if ("".equals(phone)) {
			return;
		}
		UserInfoController.getInstance().register(mHandler, u.id, phone);
		
//		new RegTask(new CallBack() {
//			
//			public void procError(String msg) {
//				Log.i("test", "注册失败");
//				count ++;
//				if (count < 3) {
//					reg(u);
//				}else {
//					Log.i("test", "超3次");	
//				}
//				
//			}
//			
//			public void procRegData(User data) {
//				Log.i("test", "注册成功");
//				saveUser(data);
//			}
//		}).execute(phone,password);
	}

	/**
	 * 
	 * @Description:TODO清空内存及文件上的数据
	 * @return void
	 * @author zhuw
	 * @date 2013-4-25 下午5:11:26
	 */
	public static void clearUser() {
		user = null;
		SpUserUtil.clearUser();

	}
	
	/**
	 * 
	 * @Description:TODO注销
	 * @return void
	 * @author zhuw
	 * @date 2013-4-26 上午10:38:12
	 */
	public static void loginOut(){
		
		clearUser();
		
	}
	/**
	 * 
	 * @Description:TODO判断是否登录
	 * @return
	 * @return boolean
	 * @author zhuw
	 * @date 2013-4-26 上午10:21:27
	 */
	public static boolean isLogin() {
		if (readUser() != null) {
			return true;
		} else {
			return false;
		}
	}

}
